/*
 * @Author: 姚潘涛
 * @Date: 2024-04-08 20:36:39
 * @LastEditors: 姚潘涛
 * @LastEditTime: 2024-04-10 09:24:26
 * @FilePath:
 * /xict_ws/src/xcmg-intelligent-cockpit-three/src/xict_ui/include/xict_ui_common/utils/msgsbridge.h
 * @Description:
 *
 * Copyright (c) 2024 by pandaman, All Rights Reserved.
 */
#ifndef MSGSBRIDGE_H
#define MSGSBRIDGE_H

#include "xict_proto/sensor/image.pb.h"
#include <QDebug>
#include <QImage>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>

namespace xict_ui_common {

    inline QImage CVMat2QImage(const cv::Mat& mat) {
        const unsigned char* data = mat.data;

        int width        = mat.cols;
        int height       = mat.rows;
        int bytesPerLine = static_cast<int>(mat.step);
        switch (mat.type()) {
        // 8 bit , ARGB
        case CV_8UC4: {
            QImage image(data, width, height, bytesPerLine,
                         QImage::Format_ARGB32);
            return image;
        }
        // 8 bit BGR
        case CV_8UC3: {
            QImage image(data, width, height, bytesPerLine,
                         QImage::Format_RGB888);
            return image.rgbSwapped();
        }
        // 8 bit Gray shale
        case CV_8UC1: {
            QImage image(data, width, height, bytesPerLine,
                         QImage::Format_Grayscale8);
            return image;
        }
        default: {
            qDebug() << "Error: cv mat type error";
            return QImage();
        }
        }
    }

    inline cv::Mat QImage2Mat(QImage image) {
        cv::Mat mat;
        switch (image.format()) {
        case QImage::Format_ARGB32:   //一般Qt读入彩色图后为此格式
            mat = cv::Mat(image.height(), image.width(), CV_8UC4,
                          (void*)image.constBits(), image.bytesPerLine());
            cv::cvtColor(mat, mat, cv::COLOR_BGRA2BGR);   //转3通道
            break;
        case QImage::Format_RGB888:
            mat = cv::Mat(image.height(), image.width(), CV_8UC3,
                          (void*)image.constBits(), image.bytesPerLine());
            cv::cvtColor(mat, mat, cv::COLOR_RGB2BGR);
            break;
        case QImage::Format_Indexed8:
            mat = cv::Mat(image.height(), image.width(), CV_8UC1,
                          (void*)image.constBits(), image.bytesPerLine());
            break;
        default:
            qDebug() << "Error: qimage type error";
            break;
        }
        return mat;
    }

    inline QImage ProtoImage2QImage(const xcmg_proto::OpencvImage& image) {
        cv::Mat cv_img(image.rows(), image.cols(), image.elt_type());
        cv_img.data = (uchar*)image.mat_data().data();
        return CVMat2QImage(cv_img);
    }

    inline xcmg_proto::OpencvImage QImage2ProtoRaw(QImage qImg) {
        cv::Mat cv_img = QImage2Mat(qImg);
        xcmg_proto::OpencvImage img;
        img.set_rows(cv_img.rows);
        img.set_cols(cv_img.cols);
        img.set_elt_type(cv_img.type());
        img.set_mat_data((char*)cv_img.data,
                         cv_img.cols * cv_img.rows * cv_img.elemSize());
        return img;
    }

}   // namespace xict_ui_common
#endif
